package insat.challenge;

import insat.challenge.metier.DBAdapter;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class ContacterActivity extends DashboardActivity implements
OnItemClickListener , OnClickListener 
{

	ListView listView;
	Button bt;
	DBAdapter db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contacter);
		
		listView = (ListView) findViewById(R.id.listView_contact);
		
		bt = (Button) findViewById(R.id.button1);
		bt.setOnClickListener(this);
		db = new DBAdapter(this);
		db.open();
		DataBind();
		listView.setOnItemClickListener(this);
		//listaction();
	}
	
	public void listaction(){
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> l, View v, int position,
					long id) {
				
		Log.i("contacterActivity","onitemclick !!");
		Cursor cursor = (Cursor) listView.getAdapter().getItem(position);
		String numTel = cursor.getString(cursor.getColumnIndex("numTel"));
		Toast.makeText(ContacterActivity.this, "Item id " + id + " : " + numTel,
				Toast.LENGTH_SHORT).show();
			
				
			}
		});
	}

	public void DataBind() {
		Cursor c = db.recupererLaListeDesContact();
		startManagingCursor(c);
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.list_item, c, new String[] { "nomprenom", "numTel" },
				new int[] { R.id.textView_nomprenom, R.id.textView2_numTel });
		listView.setAdapter(adapter);
//		listView.setOnItemClickListener(this);
//		listView.setSelection(0);
//		listView.setOnCreateContextMenuListener(this);

	}

	@Override
	// Création du menu principal
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 100, 0, "Tout effacer");
		return true;
	}

	 @Override // Creation du menu contextuel
	 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo
	 menuInfo) {
	 super.onCreateContextMenu(menu, v, menuInfo);
	 menu.setHeaderTitle("Action");
	 menu.add(0,100,0,"Supprimer");
	 menu.add(0,200,1,"Editer");
	 }
	 @Override // Selection d'un item du menu contextuel
	 public boolean onContextItemSelected(MenuItem item) {
	 AdapterView.AdapterContextMenuInfo info =
	 (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
	 switch(item.getItemId()){
	 case 100:
	 db.supprimerContact(info.id);
	 DataBind();
	 break;
	 case 200:
	 Toast.makeText(this, "TODO", Toast.LENGTH_SHORT).show();
	 break;
	 }
	 return true;
	 }

	@Override
	// Selection d'un item du menu
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 100:
			db.Truncate();
			DataBind();
			break;
		}
		return true;
	}

	@Override
	protected void onDestroy() {
		db.close();
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		startActivity(new Intent(ContacterActivity.this,
				AjouterContactActivity.class));
	}

	// ListView l, View v, int position, long id
	@Override
	public void onItemClick(AdapterView<?> l, View v, int position, long id) {
		Log.i("contacterActivity","onitemclick !!");
		Cursor cursor = (Cursor) listView.getAdapter().getItem(position);
		//Cursor cursor = (Cursor) ((Menu) listView).getItem(position);
		String numTel = cursor.getString(cursor.getColumnIndex("numTel"));
		Toast.makeText(this, "Item id " + id + " : " + numTel,
				Toast.LENGTH_SHORT).show();

	}
//	@Override
//	protected void onListItemClick(ListView l, View v, int position, long id) {
//		Log.i("contacterActivity","onitemclick !!");
//		Cursor cursor = (Cursor) ((Menu) listView).getItem(position);
//		String numTel = cursor.getString(cursor.getColumnIndex("numTel"));
//		Toast.makeText(this, "Item id " + id + " : " + numTel,
//				Toast.LENGTH_SHORT).show();
//	}
	

}
